/*
 * @Author: Your name
 * @Date:   2020-07-30 08:49:22
 * @Last Modified by:   Your name
 * @Last Modified time: 2020-08-12 10:07:05
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('views/login/login')
const Home = () => import('views/home/home')
const Welcome = () => import('views/home/childComp/Welcome')
const User = () => import('views/user/User')
const Rights = () => import('views/power/Rights')
const Roles = () => import('views/power/Roles')
const Cate = () => import('views/goods/Cate')
const Params = () => import('views/goods/Params')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: User
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由导航守卫控制登录权限
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.localStorage.getItem('logintoken')
  if (!tokenStr) return next('/login')
  next()
})
export default router
